from fileinput import filename
import time
from datetime import datetime, timedelta, timezone
from distutils import file_util
from distutils.command.upload import upload
from genericpath import isdir
import os
# from os import startfile
import config
import uuid
import re
import openpyxl
import mysql.connector

# set beijing time as default utc_time
SHA_TZ = timezone(
    timedelta(hours=8),
    name='Asia/Shanghai',
)
utc_now = datetime.utcnow().replace(tzinfo=timezone.utc)

data_list = list()

def query_all():
    cnx = None
    cursor = None
    try:
        cnx = mysql.connector.connect(**config.kkomia_picture_db)
        cursor = cnx.cursor()
    except Exception as e:
        print("Connected failed:" + str(e))
        if cursor:
            cursor.close()
        if cnx:
            cnx.close()
    
    print("Conneted to kkomia_picture_db Database")
    try:
        query = ("SELECT id, picture_name, picture_format, utc_date_time, file_object FROM tbl_picture_file")

        cursor.execute(query, )
        rows = cursor.fetchall()

    except Exception as e:
        print("Corsor.execute failed:" + str(e))
        if cursor:
            cursor.close()
        if cnx:
            cnx.close()


    for row in rows:
        data_list.append({"id": row[0],
                          "picture_name": row[1],
                          "picture_format": row[2],
                          "utc_date_time": row[3],
                          "file_object": row[4]})

    if len(data_list) == 0:
        print("kkomia_picture_db has no picture data...")
    for data in data_list:
        print("picture_id is " + str(data["id"]) + ", picture_name is " + data["picture_name"] + ", picture_format is " + data["picture_format"] + "...")

    cursor.close()
    cnx.close()
    print("Closed the database")
    return data_list



        
